package com.compass.doctor.ui.patient.view

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.ViewPatientBasicInformationBinding
import com.compass.doctor.ui.patient.PatientChatActivity
import com.compass.doctor.ui.patient.PractitionerToPatientChatActivity
import com.compass.doctor.ui.patient.adapter.PatientTagAdapter
import com.compass.framework.base.BaseActivity
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.glide.setUrlCircle
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.PatientInfo
import com.compass.framework.model.Tag
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.LogUtil
import com.netease.yunxin.kit.corekit.im.model.UserInfo

/**
 * Description： 患者基本信息view
 * Date:2023/12/14 9:29
 */
class PatientBasicInformationView(context: Context) : LinearLayout(context) {

    private var binding: ViewPatientBasicInformationBinding? = null
    private var tagAdapter: PatientTagAdapter
    private var patientInfo: PatientInfo? = null
    private var onCustomButtonClick: OnCustomButtonClick? = null

    init {
        binding =
            ViewPatientBasicInformationBinding.inflate(LayoutInflater.from(context), this, true)
        tagAdapter = PatientTagAdapter()
        binding?.rcyTag?.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = tagAdapter
        }
        binding?.mineNext?.onClick {
            patientInfo?.let {
                //没时间了 下个版本优化
                if (UserInfoManager.getUserType() == "1") {
                    PatientChatActivity.start(
                        context,
                        UserInfo(it.wyyxAccId, it.name, it.avatar), it.id
                    )
                } else {
                    PractitionerToPatientChatActivity.start(
                        context,
                        UserInfo(it.wyyxAccId, it.name, it.avatar),
                        it.id
                    )
                }

            }
        }

        binding?.tvPhone?.onClick {
            patientInfo?.let {
                val clipboardManager =
                    context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                val clipData = ClipData.newPlainText("text", patientInfo!!.mobile)
                clipboardManager.setPrimaryClip(clipData)
                if(context is BaseActivity) context.showToast("复制成功")
            }
        }

        binding?.minePhone?.onClick {
            //点击后跳转至拨号页面
            patientInfo?.let {
                val intent = Intent()
                intent.action = Intent.ACTION_DIAL
                intent.data =
                    Uri.parse("tel:${patientInfo!!.mobile}")
                context.startActivity(intent)
            }
        }

        binding?.editTag?.onClick {
            onCustomButtonClick?.onButtonCallBack(1, "add")
        }

    }

    fun setPatientInfo(patientInfo: PatientInfo) {
        this.patientInfo = patientInfo
        binding?.apply {
            ivHead.setUrlCircle(patientInfo.avatar)
            tvPhone.text = patientInfo.mobile
            patientName.text = patientInfo.name
            usefulSex.text = patientInfo.getSexString()
            usefulAge.text = patientInfo.patientAge
            tvJoinTime.text =
                DateUtils.conversionTime(patientInfo.createTime, DateUtils.FORMAT_FOUR)
            when (patientInfo.source) {
                1 -> {
                    usefulType.text = "扫码进入"
                }

                else -> {
                    usefulType.text = "自主进入"
                }
            }
        }
    }

    fun setPatientTags(patientTags: ArrayList<Tag>) {
        if (patientTags.isNotEmpty()) {
            binding?.rcyTag?.visible()
            binding?.emptyTag?.gone()
            tagAdapter.setData(patientTags)
        } else {
            binding?.rcyTag?.gone()
            binding?.emptyTag?.visible()
        }

    }

    fun setCallBack(onCustomButtonClick: OnCustomButtonClick) {
        this.onCustomButtonClick = onCustomButtonClick
    }
}